{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7ebe3a26-dad5-4119-a2b6-50f64502ca1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 命名元组,自定义元组\n",
    "from collections import namedtuple\n",
    "Point = namedtuple('Point', ['x', 'y'])\n",
    "p1 = Point(11, y=22)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b8db205d-f621-49e0-9e1c-aa95c42f47fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以使用 . 的方式访问元组元素\n",
    "p1.x is p1[0]\n",
    "p2 = Point(x=33,y=44)\n",
    "\n",
    "p2 + p1\n",
    "\n",
    "# 元组解包\n",
    "x,y,z = (1,2,3)\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2c1ccbad-e3ee-4e9e-ac35-781e3679f51f",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Point_2D(namedtuple('Point', ['x', 'y'])):\n",
    "    # 重写命名元组 + 运算\n",
    "    # other 就是 + 后面对象\n",
    "    def __add__(self, other):\n",
    "        # 元组解包\n",
    "        self.other_x, self.other_y = other\n",
    "        return self.x+self.other_x, self.y+self.other_y\n",
    "    # 重写命名元组 - 运算\n",
    "    def __sub__(self, other):\n",
    "        self.other_x, self.other_y = other\n",
    "        return self.x-self.other_x, self.y-self.other_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "796a3e53-f4f0-43d9-b770-bed9f23b48e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = Point_2D(11, y=22)\n",
    "p2 = Point_2D(x=33, y=44)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0549b70e-973e-446b-9ea9-bee4aeb26a9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-22, -22)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1 + p2\n",
    "p1 - p2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79756889",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import deque   # 双端队列\n",
    "from collections import Counter # 计数器，可以统计元素数量也可以统计元素出现次数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e6c39a07-1ee0-411c-9439-b7b51c2b0e26",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典、列表子类化\n",
    "# 自定义字典、列表\n",
    "from collections import UserDict\n",
    "\n",
    "class MyDict(UserDict):\n",
    "    def __setitem__(self, key, value):\n",
    "        # self.data 字典\n",
    "        if key in self.data.keys():\n",
    "            print(f\"字典里已经有key {key} ,不能覆盖\")\n",
    "        else:\n",
    "            self.data[str(key)] = value\n",
    " \n",
    "\n",
    "dict1 = MyDict()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5a550e13-657c-49c7-bd59-e4a1e870c818",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict1['a'] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0cd3786a-4e12-4967-9623-adeec82acb79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字典里已经有key a ,不能覆盖\n"
     ]
    }
   ],
   "source": [
    "dict1['a'] = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9bdd0b99-744a-4187-968c-68170a1475b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict1['a']"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
